<ul class="nav nav-tabs" id="tabs">
    <?php 
    $i=1;
    foreach($tables['comp'] as $comp)
    {
        echo '<li'.(($i===1) ? ' class="active"' : '').'><a href="#comp-'.$i.'" data-toggle="tab">'.$comp['code'].'</a></li>'; 
        $i++;
    }
    ?>
</ul>
<div class="tab-content">
    <?php 
    $i=1;
    foreach($tables['comp'] as $comp)
    {
        echo '<div id="comp-'.$i.'" class="tab-pane'.(($i===1) ? ' active' : '').'">'
                .'<h2>'.$comp['name'].'</h2>'
                .'<table class="no-gutter table table-striped table-condensed tablesorter span8 tab-league">'
                .'<thead>'
                    .'<tr>'
                        .'<th>Lp.</th>'
                        .'<th class="large">Nazwa</th>'
                        .'<th class="tiny">Mecze</th>'
                        .'<th class="tiny">Punkty</th>'
                        .'<th class="tiny">Zdobyte</th>'
                        .'<th class="tiny">Stracone</th>'
                        .'<th class="tiny">Różnica</th>'
                    .'</tr>'
                .'</thead>'
                .'<tbody>';

        foreach($comp['league'] as $w)
        {
            $j++;
            foreach($w['teams'] as $tk=>$tv)
            {
                $sort_points[]  = $tv['points'];
                $sort_goaldiff[]= $tv['goal_for']-$tv['goal_against'];
            }
            array_multisort($sort_points,SORT_DESC,$sort_goaldiff,SORT_DESC,$w['teams']);
            foreach($w['teams'] as $t)
            {
                echo '<tr>'
                        .'<td>'.$j.'</td>'
                        .'<td>'.($t['id']==$this->config->item('ut_team') ? '<strong>'.$t['name'].'</strong>' : $t['name']).'</td>'
                        .'<td>'.$t['played'].'</td>'
                        .'<td>'.$t['points'].'</td>'
                        .'<td>'.$t['goalfor'].'</td>'
                        .'<td>'.$t['goalaga'].'</td>'
                        .'<td>'.($t['goalfor']-$t['goalaga']).'</td>'
                    .'</tr>';
                $j++;
            }
        }
        echo '</tbody>'
            .'</table>'
            .'</div>';
        $i++;
    }
    ?>
</div>